package com.egu.service.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.egu.common.util.DateUtil;
import com.egu.common.util.UserUtil;
import com.egu.mapper.NoticeInfoMapper;
import com.egu.model.NoticeInfo;
import com.egu.model.NoticeInfoExample;
import com.egu.model.NoticeInfoExample.Criteria;
import com.egu.model.User;
import com.egu.model.UserRole;
import com.egu.service.NoticeInfoService;
import com.egu.service.UserRoleService;
import com.egu.vo.PageParam;
import com.egu.vo.RequestPageVo;
import com.egu.vo.RequestResultVo;

@Service
public class NoticeInfoServiceImpl implements NoticeInfoService {
	@Autowired
	NoticeInfoMapper noticeInfoMapper;

	@Autowired
	UserRoleService userRoleService;

	public RequestPageVo<NoticeInfo> getList(PageParam param, NoticeInfo noticeInfo) {
		RequestPageVo<NoticeInfo> result = new RequestPageVo<NoticeInfo>();
		User currentUser = UserUtil.getCurrentUser();
		NoticeInfoExample example = new NoticeInfoExample();
		Criteria criteria = example.createCriteria();
		criteria.andIsUsedEqualTo("1");
		//
		UserRole userRoleByCurrentUser = userRoleService.getUserRoleByCurrentUser(UserUtil.getCurrentUser());
		if (userRoleByCurrentUser.getRolePower()==1) {
			criteria.andUserIdEqualTo(currentUser.getId());
		}
		if (StringUtils.isNotBlank(noticeInfo.getNoticeItem())) {
			criteria.andNoticeItemLike("%" + noticeInfo.getNoticeItem() + "%");
		}
		int count = (int) noticeInfoMapper.countByExample(example);
		if (count > 0) {
			example.setStartPage(param.getOffset());
			example.setPageSize(param.getLimit());
			List<NoticeInfo> list = noticeInfoMapper.selectByExample(example);
			result.setRows(list);
			result.setTotal(count);
		}
		return result;
	}

	@Override
	@Transactional
	public RequestResultVo<NoticeInfo> saveOrUpdate(NoticeInfo noticeInfo) {
		User currentUser = UserUtil.getCurrentUser();
		if (noticeInfo.getId() == null) {
			noticeInfo.setUserId(currentUser.getId());
			noticeInfo.setUserName(currentUser.getUserName());
			noticeInfoMapper.insertSelective(noticeInfo);
		} else {
			noticeInfoMapper.updateByPrimaryKeySelective(noticeInfo);
		}
		return RequestResultVo.success();
	}

	@Override
	@Transactional
	public RequestResultVo<NoticeInfo> deleteDataById(Integer id) {
		NoticeInfo customer = new NoticeInfo();
		customer.setIsUsed("0");
		customer.setId(id);
		try {
			noticeInfoMapper.updateByPrimaryKeySelective(customer);
		} catch (Exception e) {
			return RequestResultVo.error("数据库删除失败");
		}
		return RequestResultVo.success();
	}

	@Override
	public List<NoticeInfo> getRecentList() {
		NoticeInfoExample example = new NoticeInfoExample();
		Criteria criteria = example.createCriteria();
		criteria.andIsUsedEqualTo("1");
		criteria.andNoticeEndtimeGreaterThan(DateUtil.getCurrentDate());
		List<NoticeInfo> list = noticeInfoMapper.selectByExample(example);
		return list;
	}

}
